﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using FRS.Devices;

namespace FRS
{
    public partial class DeviceList : Form
    {
        private readonly List<SwitchBox> sb_devices;
        private readonly List<CECC> cecc_devices;
        private readonly List<NETIO_230B> netio_230b_devices;
        
        private bool init = true;

        public delegate void FormHiddenHandler(object sender, EventArgs e);
        public event FormHiddenHandler FormHidden;
        private PersistentSettings data;

        public DeviceList(List<SwitchBox> sb_devices, List<CECC> cecc_devices, List<NETIO_230B> netio_230b_devices, PersistentSettings data)
        {
            InitializeComponent();
            
            this.data = data;

            this.sb_devices = sb_devices;
            this.cecc_devices = cecc_devices;
            this.netio_230b_devices = netio_230b_devices;
        }

        protected override void OnActivated(EventArgs e)
        {
            if (init)
                AddDevices();

            init = false;
            
            base.OnActivated(e);
        }
        
        private void AddDevices ()
        {
            foreach (SwitchBox device in sb_devices)
            {
                AddDeviceTree(device);
            }

            foreach (CECC device in cecc_devices)
            {
                AddDeviceTree(device);
            }
            
            foreach (NETIO_230B device in netio_230b_devices)
            {
                AddDeviceTree(device);
            }
        }

        //Add a device
        private void button1_Click(object sender, EventArgs e)
        {
            /* CECC*/
            if (radioButton1.Checked)
            {
                CECC cecc = new CECC(ipAddressControl1.Text, textBox1.Text);
                cecc_devices.Add(cecc);
                AddDeviceTree(cecc);
            }
            else if (radioButton2.Checked)
            {
                SwitchBox switchBox = new SwitchBox(ipAddressControl1.Text, textBox1.Text);
                sb_devices.Add(switchBox);
                AddDeviceTree(switchBox);
            }
            else
            {
                NETIO_230B netio_230b = new NETIO_230B(ipAddressControl1.Text, textBox1.Text);
                netio_230b_devices.Add(netio_230b);
                AddDeviceTree(netio_230b);
            }
        }
    
        private void AddDeviceTree (DeviceBase deviceBase)
        {
            treeView1.Nodes.Add(new DeviceTreeNode(deviceBase) { Text = String.Format("{0} ({1})", deviceBase.Name, deviceBase.Address) });
        }

        private void RemoveDeviceTree(DeviceBase deviceBase)
        {
            foreach (TreeNode node in treeView1.Nodes)
                if (node.Text.Equals(String.Format("{0} ({1})", deviceBase.Name, deviceBase.Address)))
                {
                    treeView1.Nodes.Remove(node);
                    break;
                }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            HideSettings();
        }
        
        private void treeView1_KeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.Delete)
            {
                DeviceTreeNode deviceTreeNode = treeView1.SelectedNode as DeviceTreeNode;
                if (deviceTreeNode != null)
                {
                    if (deviceTreeNode.Device.GetType() == typeof(CECC))
                    {
                        cecc_devices.Remove((CECC) deviceTreeNode.Device);
                    }
                    else if (deviceTreeNode.Device.GetType() == typeof(SwitchBox))
                    {
                        sb_devices.Remove((SwitchBox)deviceTreeNode.Device);
                    }
                    else if (deviceTreeNode.Device.GetType() == typeof(NETIO_230B))
                    {
                        netio_230b_devices.Remove((NETIO_230B)deviceTreeNode.Device);
                    }

                    RemoveDeviceTree(deviceTreeNode.Device);
                }
            }
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            HideSettings();
        }

        private void HideSettings()
        {
            Hide();

            init = true;
            
            FileManager.CECC_WriteDeviceListFile(cecc_devices.Select(device => new Device(device.Address, device.Name)).ToList());
            FileManager.SwitchBox_WriteDeviceListFile(sb_devices.Select(device => new Device(device.Address, device.Name)).ToList());
            FileManager.NETIO_230B_WriteDeviceListFile(netio_230b_devices.Select(device => new Device(device.Address, device.Name)).ToList());
            FileManager.WriteSettingsListFile(data);
            
            treeView1.Nodes.Clear();

            if (FormHidden != null)
                FormHidden(this, new EventArgs());
        }
    }
}
